import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/knowledgeword.dart';
import 'package:ichinesereader/models/book/BookItem.dart';
import 'package:ichinesereader/models/knowledgeword/knowledgeDetailsResult.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';

class BookKnowledgesViewer extends StatelessWidget {
  BookKnowledgesViewer({Key? key, required this.bookItem}) : super(key: key);
  final BookItem bookItem;
  static const String routeName = '/book/knowledges';

  @override
  Widget build(BuildContext context) {
    Log.debug('build common book knowledges details');
    final KnowledgeWordsController _knowledgewordsController = Get.put(KnowledgeWordsController());
    _knowledgewordsController.fetchKnowledgeDetailsByBookcode(
        bookItem.bookCode.toString(), true, true);

    return Scaffold(
      appBar: CustomAppBar(title: bookItem.bookTitle.toString()),
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: Padding(
          padding: const EdgeInsets.fromLTRB(50, 16.0, 50, 0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _knowledgewordsController.obx(
                (data) {
                  var knowledges = _knowledgewordsController
                      .knowledgeDetailsResult.value.result;
                  String _fulltext = '';
                  if (knowledges?.grammar != null &&
                      knowledges!.grammar!.length > 0) {
                    _fulltext = '语法：\n';
                    if (knowledges.grammar!.length > 0) {
                      knowledges.grammar!.sort((a, b) {
                        var end1 = a.subCategory.toString();
                        var end2 = b.subCategory.toString();
                        return end1.compareTo(end2);
                      });
                      knowledges.grammar?.forEach((grammar) {
                        _fulltext +=
                            '${grammar.subCategory}  ${grammar.kind}  ${grammar.text}\n';
                      });
                    }
                  } else {
                    _fulltext = 'Grammar data not available\n';
                  }

                  if (knowledges?.word != null &&
                      knowledges!.word!.length > 0) {
                    // _fulltext += '\n\nHandwriting\n';
                    if (knowledges.word!.length > 0) {
                      for (int i = 1; i < 4; i++) {
                        String temp = '';
                        List<KnowledgeHandwritingWord>? hwords = knowledges.word
                            ?.where((element) =>
                                element.handWriting == i.toString())
                            .toList();
                        if (hwords != null) {
                          if (hwords.isNotEmpty) {
                            if (i == 1) temp = '\n初等手写汉字：\n';
                            if (i == 2) temp = '\n中等手写汉字：\n';
                            if (i == 3) temp = '\n高等手写汉字：\n';
                            final ids = Set();
                            hwords[0].list!.retainWhere((x) => ids.add(x.id));
                            hwords[0].list!.forEach((word) {
                              temp += '${word.text.toString()}、';
                            });
                          }
                        }
                        if (temp.length > 1) {
                          temp = temp.substring(0, temp.length - 1);
                        }
                        _fulltext += temp;
                      }
                    }
                  } else {
                    _fulltext +=
                        '\nHandwriting characters data not available\n';
                  }
                  return Text(_fulltext,
                      style: TextStyle(height: 2, fontSize: 23));
                },
                onEmpty: Center(child: Text('Data not available ...'.tr)),
                onLoading: Center(
                    child: CircularProgressIndicator(
                  backgroundColor: Get.theme.primaryColorDark,
                  strokeWidth: 9.0,
                )),
              )
            ],
          ),
        ),
      ),
    );
  }
}
